package com.teamin.web.socket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
 *
 * Created by qfu on 2017/10/23.
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry
                .addHandler(new LiteWebSocketHandler(), "/stomp") //设置消息的处理者
                .setAllowedOrigins("*")//允许所有来源
                .addInterceptors(new HttpSessionHandshakeInterceptor()) //添加拦截器
        //.withSockJS() //启用sockJS
        ;
    }

    @Bean
    public WebSocketHandler myHandler() {
        return new LiteWebSocketHandler();
    }

    /**
     * 配置WebSocket引擎
     * @return
     */
    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxTextMessageBufferSize(81920);
        container.setMaxBinaryMessageBufferSize(81920);
        return container;
    }

}
